package cn.stylefeng.guns.sys.modular.system.controller;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBrand;
import cn.stylefeng.guns.sys.modular.system.entity.BtyTxl;
import cn.stylefeng.guns.sys.modular.system.service.BtyBrandService;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdaService;
import cn.stylefeng.guns.sys.modular.system.service.BtyTxlService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * 档案管理控制器
 * @author liuy
 * @Date 2020-09-22
 */
@Controller
@RequestMapping("/btyBrand")
public class BtyBrandController extends BaseController {
    private String PREFIX = "/modular/system/btyBrand/";
    @Resource
    private BtyBrandService btyBrandService;
    @Resource
    private BtyBxdaService btyBxdaService;
    @Resource
    private BtyTxlService btyTxlService;

    /**
     * 跳转到通知列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyBrand.html";
    }
    /**
     * 获取通知列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,String dm) {
        Page<Map<String, Object>> list = this.btyBrandService.list(condition,dm);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 跳转到维护妖码列表首页
     */
    @RequestMapping("/yaoMa")
    public String yaoMa() {
        return PREFIX + "btyYaoma.html";
    }

    /**
     * 跳转到维护财务码列表首页
     */
    @RequestMapping("/cwm")
    public String cwm() {
        return PREFIX + "btyCwm.html";
    }

    @RequestMapping("/ym")
    public String ym() {
        return PREFIX + "allYaoma.html";
    }

    /**
     * 获取妖码列表
     */
    @RequestMapping(value = "/listYaoma")
    @ResponseBody
    public Object listYaoma(String condition,String dm,String flag) {
//        if(ToolUtil.isEmpty(dm)){
//            dm="111";
//        }
        Page<Map<String, Object>> list = this.btyBrandService.listYaoma(condition,dm,flag);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }


    /**
     * 获取财务码列表
     */
    @RequestMapping(value = "/listCwm")
    @ResponseBody
    public Object listCwm(String cwm,String searchAll) {
        Page<Map<String, Object>> list = this.btyBrandService.listCwm(cwm,searchAll);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 获取最大财务码
     */
    @RequestMapping(value = "/maxCwm")
    @ResponseBody
    public Object maxCwm() {
        return this.btyBrandService.getMaxCwm();
    }

    /**
     * 同步营业日
     */
    @RequestMapping(value = "/updateCwmAll")
    @ResponseBody
    public Object updateCwmAll() {
        List<Map<String, Object>> listCwmAll;
        listCwmAll=this.btyBrandService.listCwmAll();
        if(listCwmAll.size()!=0){
            listCwmAll.forEach(i -> {
                BtyBrand btyBrand=new BtyBrand();
                btyBrand.setId(Long.valueOf(String.valueOf(i.get("id"))));
                btyBrand.setKyrq(i.get("kyrq").toString());
                btyBrandService.updateById(btyBrand);
            });
        }
        listCwmAll.clear();
        listCwmAll=this.btyTxlService.listCwmAll();
        if(listCwmAll.size()!=0){
            listCwmAll.forEach(i -> {
                BtyTxl btyTxl=new BtyTxl();
                btyTxl.setId(Long.valueOf(String.valueOf(i.get("id"))));
                btyTxl.setKysj(i.get("kyrq").toString());
                btyTxlService.updateById(btyTxl);
            });
        }
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/updateCwm")
    @ResponseBody
    public Object updateCwm(BtyBrand btyBrand) {
        if (ToolUtil.isOneEmpty(btyBrand)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        //判断库中是否有重复财务码
        int count=btyBrandService.countCwm(btyBrand);
        if(count==0){
            BtyTxl btyTxl=new BtyTxl();
            btyTxl.setCtbh(btyBrand.getYaoma());
            btyTxl.setCwm(btyBrand.getCwm());
            this.btyBrandService.updateById(btyBrand);
            this.btyTxlService.updateCwms(btyTxl);
            return SUCCESS_TIP;
        }else {
            return BizExceptionEnum.AUTH_CWM_ERROR.getCode();
        }
    }

    @RequestMapping(value = "/allYaoma")
    @ResponseBody
    public Object allYaoma(String dm) {
        Page<Map<String, Object>> list = this.btyBrandService.listAllYaoma(dm);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    @RequestMapping(value = "/listSearchExport")
    @ResponseBody
    public Object listSearchExport(String dm) {
        List<Map<String, Object>> list = this.btyBrandService.listSearchExport(dm);
        return list;
    }


    /**
     * 跳转到修改档案
     */
    @RequestMapping("/btyBrand_update")
    public String deptUpdate() {
        return PREFIX + "btyBrand_edit.html";
    }
    /**
     * 修改档案
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BtyBrand btyBrand) {
        if (ToolUtil.isOneEmpty(btyBrand, btyBrand.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Boolean flag= this.btyBrandService.updateById(btyBrand);
        if(flag==true){
         btyBxdaService.updateBxdaKj(btyBrand.getCwry(),btyBrand.getDm());
        }
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/updateYaoma")
    @ResponseBody
    public Object updateYaoma(BtyBrand btyBrand) {
        if (ToolUtil.isOneEmpty(btyBrand, btyBrand.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Boolean flag= this.btyBrandService.updateById(btyBrand);
        return SUCCESS_TIP;
    }

    /**
     * 查询会计
     * @author liuy
     */
    @RequestMapping(value = "/getKj")
    @ResponseBody
    public List<BtyBrand> getDictionaryType(){
        List<BtyBrand> list = btyBrandService.kjList();
        return list;
    }

    /**
     * 档案详情
     */
    @RequestMapping("/info")
    @ResponseBody
    public Object getThemeInfo(@RequestParam Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new RequestEmptyException();
        }
        BtyBrand btyBrand=this.btyBrandService.getById(id);
        return btyBrand;
    }
    /**
     * 查询店名
     */
    @RequestMapping(value = "/getDm")
    @ResponseBody
    public List<BtyBrand> getDm(){
        List<BtyBrand> list=btyBrandService.dmList();
        return list;
    }

    @RequestMapping(value = "/getDms")
    @ResponseBody
    public List<BtyBrand> getDms(){
        List<BtyBrand> list=btyBrandService.dmLists();
        return list;
    }

    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUser(@RequestParam("file") MultipartFile file) {

        String fileName = file.getOriginalFilename();//文件名
        Boolean flag;
        JsonResult jsonResult = new JsonResult();
        try {
            flag = btyBrandService.batchImport(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return flag;

    }

}
